package com.sgu6.sgu6_machine.ui.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class SignalView extends View {

    private static final float FULL_STRENGTH = 100.0f;
    private int strength = 0;
    private String type = "";
    private int width;
    private int height;
    private Paint signalPaint;
    private Paint fullPaint;
    private Paint textPaint;
    private Path path; //信号强度边框
    private Path signalPath; //信号强度

    public SignalView(Context context) {
        super(context, null);
        init();
    }

    public SignalView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        init();
    }

    public SignalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();

    }

    private void init() {
        signalPaint = new Paint();
        signalPaint.setAntiAlias(true);
        signalPaint.setColor(Color.LTGRAY);
        signalPaint.setStyle(Paint.Style.FILL);

        fullPaint = new Paint();
        fullPaint.setAntiAlias(true);
        fullPaint.setColor(Color.WHITE);
        fullPaint.setStyle(Paint.Style.STROKE);

        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setStrokeWidth(3);//设置画笔宽度
        textPaint.setAntiAlias(true);//设置是否使用抗锯齿功能，如果使用，会导致绘图速度变慢
        textPaint.setTextAlign(Paint.Align.CENTER);//设置文字对齐方式
        textPaint.setTextSize(8);//设置文字大小

        path = new Path();
        signalPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = measureLength(widthMeasureSpec, width);
        height = measureLength(heightMeasureSpec, height);
        setMeasuredDimension(width, height);
    }

    private int measureLength(int measureSpec, int length) {
        int measureLength = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        switch (mode) {
            case MeasureSpec.AT_MOST:
                //wrap_content时size其实为父控件能给的最大长度,length相关于一个默认值的作用 Math.min
                measureLength = Math.min(size, length);
                break;
            case MeasureSpec.EXACTLY:
                //match_parent或指定如30dp时 测出来多大就多大,考虑的情况是有时候测量出的确没有空间放置此控件了
                measureLength = size;
                break;
            case MeasureSpec.UNSPECIFIED:
                measureLength = length;
                break;
        }
        return measureLength;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSignalStrength(canvas);
        drawFullStrength(canvas);
        drawNetType(canvas);

    }

    private void drawFullStrength(Canvas canvas) {
        //用路径来画出这个三角形
        path.moveTo(0, height);//起始点
        path.lineTo(width, height);//从起始点画一根线到 相应的坐标
        path.lineTo(width, 0);
        path.close();//闭合
        canvas.drawPath(path, fullPaint);
    }

    public void drawSignalStrength(Canvas canvas) {
        float percent = strength / FULL_STRENGTH;
        signalPath.moveTo(0, height);
        signalPath.lineTo(width * percent, height);
        signalPath.lineTo(width * percent, height - height * percent);
        signalPath.close();
        canvas.drawPath(signalPath, signalPaint);
    }

    public void drawNetType(Canvas canvas) {
        canvas.drawText(type, 6, 20, textPaint);
    }

    public void updateNetType(String type) {
        this.type = type;
        invalidate();
    }

    public void updateSignalStrength(int strength) {
        signalPath = new Path();
        this.strength = strength;
        invalidate();
    }
}
